package org.eclipse.emf.index.omds.core.indexer;

import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.index.internal.LogFacade;
import org.eclipse.emf.index.resource.IndexFeeder;
import org.eclipse.emf.index.resource.ResourceIndexer;
import org.eclipse.jst.j2ee.model.IModelProvider;
import org.eclipse.jst.j2ee.model.ModelProviderManager;
import org.eclipse.wst.common.internal.emfworkbench.WorkbenchResourceHelper;


public class OMSResourceIndexer implements ResourceIndexer {

	public void resourceChanged(URI resourceURI, IndexFeeder indexFeeder) {
		// translate the resource URI to a workbench file
		IFile file = WorkbenchResourceHelper.getPlatformFile(resourceURI);
		System.out.println(resourceURI.toString());
		
		// get file's project
		IProject project = file.getProject();
		
		// get the model provider of the project
		IModelProvider modelProvider = ModelProviderManager.getModelProvider(project);
		if (modelProvider == null) {
			// there is no EMF model behind the changed resource
			return;
		}
		
		Object modelObject = modelProvider.getModelObject();
		System.out.println(modelObject.toString());
		if (modelObject == null) {
			// there is no EMF model behind the changed resource
			return;
		}
		
		
		// check the type of the model
		if (modelObject instanceof org.eclipse.jst.j2ee.webapplication.Servlet) {
			// this is the model of a Web 2.5 project
			System.out.println("Servelet added : ");
			org.eclipse.jst.javaee.web.WebApp webApp = (org.eclipse.jst.javaee.web.WebApp) modelObject;
			
			// add all Servlets to the index
			List<org.eclipse.jst.javaee.web.Servlet> servlets = webApp.getServlets();
			for (org.eclipse.jst.javaee.web.Servlet servlet : servlets) {
				indexFeeder.createEObjectDescriptor(
						(EObject) servlet, 
						servlet.getServletName(), 
						((org.eclipse.jst.javaee.core.DisplayName) servlet.getDisplayNames().get(0)).getValue(), 
						null);
			}
			
			
			// add all Filters to the index
			
			// add all Listeners to the index
			// TODO Sajith
		} else if (modelObject instanceof org.eclipse.jst.j2ee.webapplication.WebApp) {
			// this is the model of a Web 2.2-2.4 project
			// TODO Sajith
			// TODO add all Servlet, Filters and Listener objects to the index
		} else if (modelObject instanceof org.eclipse.jst.javaee.ejb.EJBJar) {
			// this is the model of a EJB 3.0 project
			// TODO Sajith
			// TODO add all Session Beans and Messsage-Driven Beans to the index
		} else if (modelObject instanceof org.eclipse.jst.j2ee.ejb.EJBJar) {
			// this is the model of a EJB 1.1-2.1 project
			// TODO Sajith
			// TODO add all Session Beans, Message-Driven Beans and Entity Beans to index
		}
	}

	@Override
	public void resourceDeleted(URI resourceURI, IndexFeeder feeder) {
		try {
			feeder.deleteResourceDescriptor(resourceURI);
		} catch (Exception exc) {
			LogFacade.logError("Error deleting resource from index " + resourceURI, exc);
		}
	}

}
